﻿@{
    @*Get userid*@
    var uid = KimPhongCRM.Lib.SessionData.GetSession("sdUserId").ToString();
    Guid? user_id;
    if (uid != "")
    {
        user_id = new Guid(KimPhongCRM.Lib.SessionData.GetSession("sdUserId").ToString());
    }
    else
    {
        KimPhongCRM.Controllers.BaseController bcontrol = new KimPhongCRM.Controllers.BaseController();
        string un = User.Identity.Name;
        bcontrol.ressUserID(un);
        user_id = new Guid(KimPhongCRM.Lib.SessionData.GetSession("sdUserId").ToString());
    }
    @*Get userid*@    
    var listUser = ViewData["listUser"] as List<KimPhongCRM.Models.sp_Select_Combobox_User_Result>;
    var listLoaithe = ViewData["listLoaithe"] as List<KimPhongCRM.Models.sp_Select_Combobox_LoaiThe_Result>;
    var listLuattinhdiem = ViewData["listLuattinhdiem"] as List<KimPhongCRM.Models.sp_Select_Combobox_LuatTinhDiem_Result>;
    var grid = Html.DevExpress().GridView(settings =>
    {
        settings.Name = "LuatCapThePartial";
        settings.CallbackRouteValues = new { Controller = "LuatCapThe", Action = "ListLuatCapThePartial" };

        settings.SettingsEditing.AddNewRowRouteValues = new { Controller = "LuatCapThe", Action = "ListLuatCapThePartialAddNew" };
        settings.SettingsEditing.UpdateRowRouteValues = new { Controller = "LuatCapThe", Action = "ListLuatCapThePartialUpdate" };
        settings.SettingsEditing.DeleteRowRouteValues = new { Controller = "LuatCapThe", Action = "ListLuatCapThePartialDelete" };
        settings.CommandColumn.ButtonType = System.Web.UI.WebControls.ButtonType.Image;
        settings.CommandColumn.SetColVisibleIndex(0);
        settings.CommandColumn.Width = 100;
        settings.CommandColumn.SetHeaderCaptionTemplateContent("Tùy chọn");
        settings.CommandColumn.FixedStyle = GridViewColumnFixedStyle.Left;

        settings.CommandColumn.NewButton.Visible = true;
        settings.CommandColumn.NewButton.Image.Url = @Url.Content("~/Content/img/icon-24-new.png");
        settings.CommandColumn.NewButton.Image.ToolTip = "New";
        settings.CommandColumn.NewButton.Image.AlternateText = "Thêm";

        settings.CommandColumn.EditButton.Visible = true;
        settings.CommandColumn.EditButton.Image.Url = @Url.Content("~/Content/img/icon-24-edit.png");
        settings.CommandColumn.EditButton.Image.ToolTip = "Eidt";
        settings.CommandColumn.EditButton.Image.AlternateText = "Sửa";

        settings.CommandColumn.DeleteButton.Visible = true;
        settings.CommandColumn.DeleteButton.Image.Url = @Url.Content("~/Content/img/icon-24-delete.png");
        settings.CommandColumn.DeleteButton.Image.ToolTip = "Delete";
        settings.CommandColumn.DeleteButton.Image.AlternateText = "Xóa";

        settings.CommandColumn.UpdateButton.Visible = true;
        settings.CommandColumn.UpdateButton.Image.Url = @Url.Content("~/Content/img/icon-24-ok.png");
        settings.CommandColumn.UpdateButton.Image.ToolTip = "Update";
        settings.CommandColumn.UpdateButton.Image.AlternateText = "Lưu";

        settings.CommandColumn.CancelButton.Visible = true;
        settings.CommandColumn.CancelButton.Image.Url = @Url.Content("~/Content/img/icon-24-cancel.png");
        settings.CommandColumn.CancelButton.Image.ToolTip = "Cancel";
        settings.CommandColumn.CancelButton.Image.AlternateText = "Hủy";


        settings.SettingsEditing.Mode = GridViewEditingMode.PopupEditForm;
        settings.SettingsPopup.EditForm.AllowResize = true;
        settings.SettingsPopup.EditForm.HorizontalAlign = PopupHorizontalAlign.WindowCenter;
        settings.SettingsPopup.EditForm.VerticalAlign = PopupVerticalAlign.WindowCenter;
        settings.SettingsPopup.EditForm.Width = System.Web.UI.WebControls.Unit.Percentage(90);
        settings.SettingsPopup.EditForm.Height = System.Web.UI.WebControls.Unit.Percentage(90);
        settings.SettingsPopup.EditForm.Modal = true;
        settings.SettingsBehavior.ConfirmDelete = true;

        settings.CommandColumn.Visible = true;
        settings.CommandColumn.NewButton.Visible = true;
        settings.CommandColumn.DeleteButton.Visible = true;
        settings.CommandColumn.EditButton.Visible = true;

        settings.SettingsPager.Visible = true;
        settings.Settings.ShowGroupPanel = true;
        settings.Settings.ShowFilterRow = true;
        settings.SettingsBehavior.AllowSelectByRowClick = true;
        settings.Settings.HorizontalScrollBarMode = ScrollBarMode.Auto;
        settings.Settings.VerticalScrollBarMode = ScrollBarMode.Hidden;
        settings.Width = System.Web.UI.WebControls.Unit.Percentage(100);

        settings.KeyFieldName = "maLuat";

        settings.InitNewRow = (s, e) =>
        {
            e.NewValues["ngaySuaCuoi"] = System.DateTime.Now;
            e.NewValues["ngayNhap"] = System.DateTime.Now;
            e.NewValues["ngayApDung"] = System.DateTime.Now;
            e.NewValues["isActive"] = true;
            e.NewValues["nguoiNhap"] = user_id;
            e.NewValues["nguoiSuaCuoi"] = user_id;
        };
        settings.CellEditorInitialize = (sender, e) =>
        {
            if (e.Column.FieldName == "nguoiSuaCuoi")
            {
                e.Editor.Value = user_id;
            }
            if (e.Column.FieldName == "ngaySuaCuoi")
            {
                e.Editor.Value = System.DateTime.Now;
            }
        };
        settings.Columns.Add(column =>
        {
            column.FieldName = "maThe";
            column.Caption = "Tên thẻ";
            column.ColumnType = MVCxGridViewColumnType.ComboBox;
            var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
            if(listLoaithe!=null)
            {
                foreach (var value in listLoaithe)
                {
                    comboBoxProperties.Items.Add(value.tenThe, value.maThe);
                }
                comboBoxProperties.ValueType = typeof(int);
            }
            column.FixedStyle = GridViewColumnFixedStyle.Left;
            //column.Width = 290;
        });
        settings.Columns.Add(column =>
        {
            column.FieldName = "maLuat";
            column.Caption = "Mã luật";
            column.ColumnType = MVCxGridViewColumnType.TextBox;
            column.Visible = false;
            column.Width = 100;
        });
        settings.Columns.Add(column =>
        {
            column.FieldName = "ngayNhap";
            column.Caption = "Ngày nhập";
            column.ColumnType = MVCxGridViewColumnType.DateEdit;
            var dateProperties = column.PropertiesEdit as DateEditProperties;
            dateProperties.UseMaskBehavior = true;
            dateProperties.EditFormat = EditFormat.Custom;
            dateProperties.EditFormatString = "dd/MM/yyyy hh:mm tt";
            column.Width = 100;
        });
        settings.Columns.Add(column =>
        {
            column.FieldName = "nguoiNhap";
            column.Caption = "Người nhập";
            column.ColumnType = MVCxGridViewColumnType.ComboBox;
            var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
            foreach (var value in listUser)
            {
                comboBoxProperties.Items.Add(value.UserName, value.UserId);
            }
            comboBoxProperties.ValueType = typeof(Guid);
            column.ReadOnly = true;
            column.Width = 100;
        });
        settings.Columns.Add(column =>
        {
            column.FieldName = "ngayApDung";
            column.Caption = "Ngày áp dụng";
            column.ColumnType = MVCxGridViewColumnType.DateEdit;
            var dateProperties = column.PropertiesEdit as DateEditProperties;
            dateProperties.UseMaskBehavior = true;
            dateProperties.EditFormat = EditFormat.Custom;
            dateProperties.EditFormatString = "dd/MM/yyyy hh:mm tt";
            column.Width = 100;
        });
        settings.Columns.Add(column =>
        {
            column.FieldName = "ngaySuaCuoi";
            column.Caption = "Ngày sửa cuối";
            column.ColumnType = MVCxGridViewColumnType.DateEdit;
            var dateProperties = column.PropertiesEdit as DateEditProperties;
            dateProperties.UseMaskBehavior = true;
            dateProperties.EditFormat = EditFormat.Custom;
            dateProperties.EditFormatString = "dd/MM/yyyy hh:mm tt";
            column.ReadOnly = true;
            column.Width = 100;
        });
        settings.Columns.Add(column =>
        {
            column.FieldName = "nguoiSuaCuoi";
            column.Caption = "Người sửa cuối";
            column.ColumnType = MVCxGridViewColumnType.ComboBox;
            var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
            foreach (var value in listUser)
            {
                comboBoxProperties.Items.Add(value.UserName, value.UserId);
            }
            comboBoxProperties.ValueType = typeof(Guid);
            column.ReadOnly = true;
            column.Width = 100;
        });
        settings.Columns.Add(column =>
        {
            column.FieldName = "diemTu";
            column.Caption = "Điểm từ";
            column.ColumnType = MVCxGridViewColumnType.SpinEdit;
            var SpinEditPropertie = column.PropertiesEdit as SpinEditProperties;
            SpinEditPropertie.DisplayFormatString = "#.00";
            SpinEditPropertie.NumberType = SpinEditNumberType.Float;
            SpinEditPropertie.Increment = 1000;
            column.Width = 100;
        });
        settings.Columns.Add(column =>
        {
            column.FieldName = "isActive";
            column.Caption = "Active";
            column.ColumnType = MVCxGridViewColumnType.CheckBox;
            column.Width = 60;
            column.SetDataItemTemplateContent(c =>
            {
                bool actived = (bool)DataBinder.Eval(c.DataItem, "isActive");
                object maluat_obj = DataBinder.Eval(c.DataItem, "maLuat");
                string updateLink = Url.Action("ToogleActive", "Base", new { keyvalue = maluat_obj, active = !actived, tablename = "tblLuatCapThe", keyname = "maThe" });
                ViewContext.Writer.Write(
                    "<a href=\"" + updateLink + "\" id=\"id_button_active_" + maluat_obj + "\" class=\"button btn-active\"><i class=\"font-icon-" + (actived ? "check" : "check-empty") + "\"></i></a>"
                );
            });
        });
    });
    if (ViewData["EditError"] != null)
    {
        grid.SetEditErrorText((string)ViewData["EditError"]);
    }
}
@grid.Bind(Model).GetHtml()